package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author stone
 * @date 2023/04/05 15:27
 */
public class UserServiceImpl implements UserService {
    @Override
    public CommonData query(Integer page, Integer limit, String sort, String order, String username, String mobile) {
        MarketUserExample example=new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameEqualTo(username);
        }

        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);


        MarketUserMapper marketUserMapper = TransactionUtil.getMapper(MarketUserMapper.class);
        PageHelper.startPage(page, limit);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);
        return CommonData.data(new PageInfo(marketUsers));
    }

    @Override
    public BaseRespVo queryDetail(Integer id) {
        MarketUserExample example=new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andIdEqualTo(id);
        }

        MarketUserMapper marketUserMapper = TransactionUtil.getMapper(MarketUserMapper.class);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);
        MarketUser marketUser = marketUsers.get(0);
        return BaseRespVo.ok(marketUser);
    }
    @Override
    public MarketUser selectByPrimaryKey(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketUser marketUser = mapper.selectByPrimaryKey(id);
        return marketUser;
    }


}
